//
//  UIViewController+EmptyView.m
//  SSSMerchant
//
//  Created by 谢曦 on 2017/4/10.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "EmptyView.h"

@interface EmptyView ()
//提示图
@property (nonatomic, strong) UIImageView     *imageView;
//提示文字
@property (nonatomic, strong) UILabel         *tipLabel;
//刷新按钮
@property (nonatomic, strong) UIButton        *loadBtn;
@property (nonatomic, copy)   EmptyViewReloadDataBlock reloadBlock;
@end

@implementation EmptyView
+(EmptyView *)emptyViewWithType:(EmptyViewType)type tipText:(NSString *)tip imageName:(NSString *)imageName showLoadBtn:(BOOL)show  reloadBlock:(EmptyViewReloadDataBlock)block
{
    EmptyView *emptyView = [[EmptyView alloc]initWithFrame:CGRectZero];
    switch (type) {
        case EmptyViewTypeImageLable:
        {
            emptyView.imageView.hidden=NO;
            emptyView.tipLabel.hidden=NO;
        }
            break;
        case EmptyViewTypeImage:
        {
            emptyView.imageView.hidden=NO;
            emptyView.tipLabel.hidden=YES;
        }
            break;
        case EmptyViewTypeLable:
        {
            emptyView.imageView.hidden=YES;
            emptyView.tipLabel.hidden=NO;
        }
            break;
            
        default:
            break;
    }
    emptyView.tipText=tip;
    emptyView.showImage=[UIImage imageNamed:imageName];
    emptyView.loadBtn.hidden=!show;
    emptyView.reloadBlock=block;
    return emptyView;

    
}
-(void)showInView:(UIView *)view{
    [view addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(view);
     
    }];
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=RandomColor;
        _imageView = [[UIImageView alloc]init];
        [self addSubview:_imageView];
        
        _tipLabel = [[UILabel alloc]init];
        _tipLabel.textColor = [UIColor grayColor];
        _tipLabel.font = [UIFont systemFontOfSize:15.0];
        [_tipLabel sizeToFit];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_tipLabel];
        
        _loadBtn = [[UIButton alloc]init];
        [_loadBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _loadBtn.layer.borderColor = [UIColor grayColor].CGColor;
        _loadBtn.layer.borderWidth = 1;
        _loadBtn.layer.cornerRadius =4;
        _loadBtn.layer.masksToBounds = YES;
        [_loadBtn setTitle:@"重新加载" forState:UIControlStateNormal];
        [_loadBtn addTarget:self action:@selector(loadClick) forControlEvents:UIControlEventTouchUpInside];//父控件没有frame 导致不可点
        [self addSubview:_loadBtn];
        
        [self prepareSubView];
    }
    return self;
}

- (void)loadClick{
    if (_reloadBlock) {
        _reloadBlock();
    }
        [self removeFromSuperview];
}

//布局
- (void)prepareSubView{
    //这样设置 可以让父控件适应子控件
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
//        make.size.mas_equalTo(_imageView.image.size);
        make.width.lessThanOrEqualTo(self.mas_width).multipliedBy(0.5);
          make.height.lessThanOrEqualTo(self.mas_height).multipliedBy(0.5);
//        make.width.greaterThanOrEqualTo(@100);
//          make.height.greaterThanOrEqualTo(@100);
        make.top.equalTo(self);
    }];
    
    [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.equalTo(_imageView.mas_bottom).offset(10);
         }];
    
    [_loadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.equalTo(_tipLabel.mas_bottom).offset(10);
        make.bottom.equalTo(self);
    }];
   
    
}
-(void)setTipText:(NSString *)tipText{
    _tipText=tipText;
    _tipLabel.text=tipText;
    

}
-(void)setLoadTitle:(NSString *)loadTitle{
    _loadTitle=loadTitle;
    [_loadBtn setTitle:loadTitle forState:UIControlStateNormal];
    
}

-(void)setShowImage:(UIImage *)showImage{
    _showImage=showImage;
    _imageView.image=showImage;
}
@end

//@implementation UIView (EmptyView)
//-(void)emptyViewWithType:(EmptyViewType)type showLoadBtn:(BOOL)show andReloadBlock:(EmptyViewReloadDataBlock)block{
//     EmptyView *emptyView = [EmptyView emptyViewWithType:type showLoadBtn:show];
//    emptyView.reloadBlock = block;
//      [self addSubview:emptyView];
//    [emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.equalTo(self);
//    }];
//    
//    
//
//}
//@end
